﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Net;
using System.Runtime.Serialization.Json;

namespace ServicioLugares
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "BusquedaLugar" in code, svc and config file together.
    public class BusquedaLugar : IBusquedaLugar
    {
        public GeoResponse GetLugares(string latitud, string longitud, string nombreLugar, string tipo, string radio, int cantitems)
        {
            string url = string.Format(
                "https://maps.googleapis.com/maps/api/place/search/json?location={0},{1}&radius={2}&types={3}&name={4}&sensor=false&key=AIzaSyDHKaKN1nH5LSR27DNbeZpUI9xtI2ztPjE",
                latitud,
                longitud,
                radio,
                tipo,                
                nombreLugar
            );
            var request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse));
            var res = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream());
            GeoResponse respuesta = new GeoResponse();
            respuesta.Status = res.Status;
            respuesta.Results = res.Results != null && res.Results.Length > 0 ? res.Results.Take(cantitems).ToArray() : res.Results;
            return respuesta;
        }
    }
}
